package com.menu.web.controller.attribute;

import com.menu.common.annotation.Log;
import com.menu.common.core.controller.BaseController;
import com.menu.common.core.domain.AjaxResult;
import com.menu.common.core.page.TableDataInfo;
import com.menu.common.enums.BusinessType;
import com.menu.common.enums.DataSourceType;
import com.menu.common.utils.poi.ExcelUtil;
import com.menu.framework.datasource.DynamicDataSourceContextHolder;
import com.menu.system.domain.MenuPublicattributeData;
import com.menu.system.service.IMenuPublicattributeDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 共有类型属性Controller
 *
 * @author ruoyi
 * @date 2024-07-08
 */
@RestController
@RequestMapping("/system/publicattributeData")
public class MenuPublicattributeDataController extends BaseController {
    @Autowired
    private IMenuPublicattributeDataService menuPublicattributeDataService;

    /**
     * 查询共有类型属性列表
     */
    @PreAuthorize("@ss.hasPermi('system:type:list')")
    @GetMapping("/list")
    public TableDataInfo list(MenuPublicattributeData menuPublicattributeData) {
        startPage();
        List<MenuPublicattributeData> list = menuPublicattributeDataService.selectMenuPublicattributeDataList(menuPublicattributeData);
        return getDataTable(list);
    }

    /**
     * 导出共有类型属性列表
     */
    @PreAuthorize("@ss.hasPermi('system:type:export')")
    @Log(title = "共有类型属性", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MenuPublicattributeData menuPublicattributeData) {
        List<MenuPublicattributeData> list = menuPublicattributeDataService.selectMenuPublicattributeDataList(menuPublicattributeData);
        ExcelUtil<MenuPublicattributeData> util = new ExcelUtil<MenuPublicattributeData>(MenuPublicattributeData.class);
        util.exportExcel(response, list, "共有类型属性数据");
    }

    /**
     * 获取共有类型属性详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:type:query')")
    @GetMapping(value = "/{publicattributeCode}")
    public AjaxResult getInfo(@PathVariable("publicattributeCode") Long publicattributeCode) {

        return success(menuPublicattributeDataService.selectMenuPublicattributeDataByPublicattributeCode(publicattributeCode));
    }

    /**
     * 新增共有类型属性
     */
    @PreAuthorize("@ss.hasPermi('system:type:add')")
    @Log(title = "共有类型属性", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MenuPublicattributeData menuPublicattributeData) {

        MenuPublicattributeData menuPublicattributeDataAdd = new MenuPublicattributeData();
        menuPublicattributeDataAdd.setPublicattributeValue(menuPublicattributeData.getPublicattributeValue());
        menuPublicattributeDataAdd.setPublicattributeType(menuPublicattributeData.getPublicattributeType());

        if (!menuPublicattributeDataService.selectMenuPublicattributeDataList(menuPublicattributeDataAdd).isEmpty()) {
            return AjaxResult.warn(DynamicDataSourceContextHolder.getDataSourceType().equals(DataSourceType.MASTER.name()) ?"键值重复":"key value duplication");
        }
        return toAjax(menuPublicattributeDataService.insertMenuPublicattributeData(menuPublicattributeData));
    }

    /**
     * 修改共有类型属性
     */
    @PreAuthorize("@ss.hasPermi('system:type:edit')")
    @Log(title = "共有类型属性", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MenuPublicattributeData menuPublicattributeData) {

        MenuPublicattributeData menuPublicattributeDataAdd = new MenuPublicattributeData();
        menuPublicattributeDataAdd.setPublicattributeValue(menuPublicattributeData.getPublicattributeValue());
        menuPublicattributeDataAdd.setPublicattributeType(menuPublicattributeData.getPublicattributeType());
        List<MenuPublicattributeData> menuPublicattributeData1 = menuPublicattributeDataService.selectMenuPublicattributeDataList(menuPublicattributeDataAdd);
        if (!menuPublicattributeData1.isEmpty()) {
            for (MenuPublicattributeData publicattributeData : menuPublicattributeData1) {
                if(!menuPublicattributeData.getPublicattributeLabel().equals(publicattributeData.getPublicattributeLabel())){
                    return AjaxResult.warn(DynamicDataSourceContextHolder.getDataSourceType().equals(DataSourceType.MASTER.name()) ?"键值重复":"key value duplication");
                }
            }


        }
        return toAjax(menuPublicattributeDataService.updateMenuPublicattributeData(menuPublicattributeData));
    }

    /**
     * 删除共有类型属性
     */
    @PreAuthorize("@ss.hasPermi('system:type:remove')")
    @Log(title = "共有类型属性", businessType = BusinessType.DELETE)
    @DeleteMapping("/{publicattributeCodes}")
    public AjaxResult remove(@PathVariable Long[] publicattributeCodes) {
        return toAjax(menuPublicattributeDataService.deleteMenuPublicattributeDataByPublicattributeCodes(publicattributeCodes));
    }
}
